//
//  RegisterVC.m
//  Demo
//
//  Created by LG on 2018/10/16.
//  Copyright © 2018 LG. All rights reserved.
//

#import "RegisterVC.h"
#import "RegisterView/RegisterHeaderView.h"

@interface RegisterVC ()<
UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)RegisterVCContainerView *containerView;
@property (nonatomic, strong)UITableView *tview;
@property (nonatomic, strong)RegisterHeaderView *headerView;
@property (nonatomic,assign)CGRect originalHeaderViewFrame;
@property (nonatomic,assign)CGRect originalTableViewFrame;
@property (nonatomic,assign)BOOL isFoldAnimating;

@end

@implementation RegisterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"积分中心";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.containerView];
    
    self.headerView.frame = CGRectMake(0, 0, kScreenWidth, [RegisterHeaderView heightForOriginal]);
    self.headerView.tag = RegisterVCContainerViewSubviewTagRegisterView;
    [self.containerView addSubview:self.headerView];
    self.originalHeaderViewFrame = self.headerView.frame;
    
    self.tview.backgroundColor = [UIColor clearColor];
    self.tview.tag = RegisterVCContainerViewSubviewTagTableView;
    [self.containerView addSubview:self.tview];
    self.originalTableViewFrame = self.tview.frame;
    
    UIView *header = [UIView new];
    header.backgroundColor = [UIColor clearColor];
    header.frame = self.headerView.bounds;
    header.userInteractionEnabled = NO;
    self.tview.tableHeaderView = header;
}

- (void)foldCallBack:(BOOL)fold
{
    CGRect frame = self.headerView.frame;
    frame.size.height -= [CalendarView viewHeightWithFold:!fold] - [CalendarView viewHeightWithFold:fold];
//    self.headerView.frame = frame;
//    CGRect categoryFrame = self.headerView.view_category.frame;
//    categoryFrame.origin.y -= [CalendarView viewHeightWithFold:!fold] - [CalendarView viewHeightWithFold:fold];
    
    self.isFoldAnimating = YES;
    
    [UIView animateWithDuration:.4 animations:^{
        self.tview.contentOffset = CGPointMake(0, self.tview.contentOffset.y+[CalendarView viewHeightWithFold:!fold] - [CalendarView viewHeightWithFold:fold]);
    } completion:^(BOOL finished) {
//        [self.tview performBatchUpdates:^{
//            self.tview.tableHeaderView.frame = frame;
//        } completion:^(BOOL finished) {
            self.isFoldAnimating = NO;
//        }];
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
//            self.tview.tableHeaderView = self.tview.tableHeaderView;
//        });
    }];
}

#pragma UITableViewDelegate,UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%i",(int)indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 100;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.isFoldAnimating) {
        return;
    }
    CGFloat contentOffsetY = scrollView.contentOffset.y;
    CGRect frame = self.originalHeaderViewFrame;
    frame.origin.y = frame.origin.y - contentOffsetY;
    self.headerView.frame = frame;
}

#pragma mark -- Getter

- (RegisterVCContainerView *)containerView
{
    if (!_containerView) {
        _containerView = [[RegisterVCContainerView alloc]initWithFrame:self.view.bounds];
    }
    
    return _containerView;
}

- (UITableView *)tview
{
    if (!_tview) {
        _tview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tview.delegate = self;
        _tview.dataSource = self;
    }
    
    return _tview;
}

- (RegisterHeaderView *)headerView
{
    if (!_headerView) {
        CGFloat height = [RegisterHeaderView heightForOriginal];
        _headerView = [[RegisterHeaderView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds),height)];
        
        __weak typeof(self)wSelf = self;
        _headerView.calendarView.foldCallBack = ^(BOOL fold) {
            __strong typeof(wSelf)sSelf = wSelf;
            [sSelf foldCallBack:fold];
        };
    }
    
    return _headerView;
}

@end


@implementation RegisterVCContainerView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *registerView = [self viewWithTag:RegisterVCContainerViewSubviewTagRegisterView];
    CGRect registerFrame = [self convertRect:registerView.frame toView:self];
    
    if (CGRectContainsPoint(registerFrame, point)) {
        UIView * handleView = [registerView hitTest:point withEvent:event];
        if (handleView) {
            return handleView;
        }
    }
    return [super hitTest:point withEvent:event];
}

@end
